Walkthrough 11-3: Transform complex data structures with arrays
In this walkthrough, you create a new flow that allows multiple flights to be posted to it, so you have more complex data with which to work. You will:
· Create a new flow that receives POST requests of a JSON array of flight objects.
· Transform a JSON array of objects to DataWeave, JSON, and Java.
Starting file
If you did not complete the previous walkthrough, you can get a starting file here. This file is also located in the solutions folder of the student files ZIP located in the Course Resources.
Create a new flow that receives POST requests of a JSON array of flight objects
1. Return to implementation.xml.
2. Drag out an HTTP Listener from the Mule Palette to the bottom of the canvas.
3. Change the flow name to postMultipleFlights.
4. In the HTTP Listener properties view, set the display name to POST /multipleflights.
5. Ensure the connector configuration is set to the existing HTTP_Listener_config.
6. Set the path to /multipleflights and set the allowed methods to POST.
Note: You are bypassing the APIkit router because a corresponding resource/method pair is not defined in the API.
7. Add a Transform Message component to the flow.
8. In the Transform Message properties view, set the expression to payload.
9. Add a Logger to the flow.
Add input metadata for the transformation
10. In the Transform Message properties view, click Define metadata in the input section.
11. In the Select metadata type dialog box, select flights_json and click Select.
12. In the Transform Message Properties view, you should now see metadata for the input.
Preview sample data and sample output
13. In the input section, right-click the payload and select Edit Sample Data to get sample input data.
14. Click the Preview button to get sample output data.
If sample data doesn't automatically appear in the preview section, click the Create required sample data to execute preview link.
15. Look at the preview section; the sample output should be an ArrayList of LinkedHashMaps.
16. Change the output type to application/dw.
Return values for the first object in the collection
17. Change the DataWeave expression to payload[0].
18. Change the DataWeave expression to payload[0].price.
19. Change the DataWeave expression to payload[0].*price.
Return collections of values
20. Change the DataWeave expression to return a collection of all the prices.
21. Change the DataWeave expression to payload.price; you should get the same result.
22. Change the DataWeave expression to return a collection of prices and available seats.
[payload.price, payload.emptySeats]
Use the map operator to return object collections with different data structures
23. Change the DataWeave expression to payload map $.
24. Change the DataWeave expression to set a property called flight for each object that is equal to its index value in the collection.
25. Change the DataWeave expression to create a property called destination for each object that is equal to the value of the toAirportCode field in the payload collection.
26. Change the DataWeave expression to dynamically add each of the properties present on the payload objects.
27. Remove the first flight property assignment.
28. Change the DataWeave expression so the name of each object is flight+index and you get flight0 and flight1.
Use explicit named parameters with the map operator
29. Change the map operator so that it uses two parameters called object and index and set the field name to just the index value.
30. Change the DataWeave expression so the name of each object is flight+index and you get flight0 and flight1.
Note: If you want to test the application, change the output type to application/json and then in Advanced REST Client, make a request to http://localhost:8081/multipleflights and replace the request body with JSON from the flights-example.json file.
Change the expression to output different data types
31. Change the DataWeave expression output type from application/dw to application/json.
32. Change the output type to application/java.
33. Change the output type to application/xml; you should get issues displayed.
34. Click the warning icon above the code, review the issues, then click OK.
35. Change the DataWeave expression to create a root node called flights; you should still get issues.
36. Click the warning icon above the code, review the issues, then click OK.